package com.sfac.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.aop.MyAnnotation;
import com.sfac.modules.common.vo.Result;
import com.sfac.modules.common.vo.Search;
import com.sfac.modules.test.pojo.City;
import com.sfac.modules.test.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api")
public class CityControlller {
    @Autowired
    private CityService cityService;

    /**
     * 添加城市方法(consumes是声明传参是通过JSON形式传参)
     * @param city
     * @return
     */
    @PostMapping(value = "/city",consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }


    /**
     * 添加城市方法(consumes是声明传参是通过表单形式传参)
     * @param city
     * @return
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }


    /**
     * 删除城市方法
     * @param cityId
     * @return
     */
    @DeleteMapping(value = "/city/{cityId}")
    public Result<City> deleteCity(@PathVariable int cityId){
        return cityService.deleteCity(cityId);
    }

    /**
     * 查询城市方法
     * @param cityId
     * @return
     */
    @GetMapping(value = "/city/{cityId}")
    public Result<City> selectCity(@PathVariable int cityId){
        return cityService.selectCity(cityId);
    }


    /**
     * 查询所有城市
     * @return
     */
    @GetMapping(value = "/all/cities")
    @MyAnnotation
    public Result<City> selectCity(){
        return cityService.selectAllCitys();
    }

    /**
     * 按名称查询城市信息
     * @param cityName
     * @param localCityName
     * @return
     */
    @GetMapping(value = "city")
    public Result<City> selectCityByName(@RequestParam String cityName,
                                         @RequestParam String localCityName){
        return cityService.getCityName(cityName,localCityName);
    }


    /**
     * 分页查询
     * @param search
     * @return
     */
    @PostMapping(value = "/cities",consumes = "application/json")
    public PageInfo<City> getCitiesBySearch(@RequestBody Search search){
        return cityService.getCitiesBySearch(search);
    }

}
